package com.lyy.mapper;

import com.lyy.entity.Chapter;
import com.lyy.entity.Video;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author Liangyaoyun
 */
@Repository
public interface VideoMapper {

    String list = "select v.video_id, v.video_title, v.author, v.video_info, v.summary_image, " +
            "v.cover_image, v.price, v.star, v.create_time, v.modify_time from lesson.video v ";

    /**
     * 按条件查询视频记录，分步联查该视频下的全部章记录，以及每一章下的全部集记录
     *
     * @param condition 查询条件
     * @return 查询失败时返回空List对象
     */
    @Results(id="withChapters", value = {
            @Result(id = true, column = "video_id", property = "videoId"),
            @Result(column = "video_id", property = "chapters",
                    many = @Many(select = "com.lyy.mapper.ChapterMapper.selectByVideoId"))
    })
    @Select("<script>" + list +
            "<where> " +
            "<if test='videoId != null'>and v.video_id = #{videoId}</if> " +
            "<if test='videoTitle != null'>and v.video_title = #{videoTitle}</if> " +
            "<if test='author != null'>and v.author = #{author}</if> " +
            "<if test='videoInfo != null'>and v.video_info = #{videoInfo}</if> " +
            "<if test='summaryImage != null'>and v.summary_image = #{summaryImage}</if> " +
            "<if test='coverImage != null'>and v.cover_image = #{coverImage}</if> " +
            "<if test='price != null'>and v.price = #{price}</if> " +
            "<if test='star != null'>and v.star = #{star}</if> " +
            "<if test='createTime != null'>and v.create_time = #{createTime}</if> " +
            "<if test='modifyTime != null'>and v.modify_time = #{modifyTime}</if> " +
            "</where> " +
            "</script> ")
    List<Video> select(Video condition);

    /**
     * 按视频表主键查询一条视频记录（分步调用专用SQL块），分步联查该视频下的全部章记录，以及每一章下的全部集记录
     *
     * @param videoId 视频表主键
     * @return 查询失败时返回null
     */
    @ResultMap("withChapters")
    @Select(list + " where v.video_id = #{param1} ")
    Video selectByVideoId(Integer videoId);

    /**
     * 按视频表主键数组查询视频记录
     *
     * @param videoIds 视频表主键数组
     * @return 查询失败时返回空List对象
     */
    @Select("<script>" + list + " where " +
            "<foreach collection='array' item='videoId' open='v.video_id in (' close=')' " +
            "separator=',' > #{videoId} </foreach> " +
            "</script>")
    List<Video> selectByVideoIds(Integer[] videoIds);

    /**
     * 按 [视频标题] 模糊查询 [视频]
     * @param videoTitle 视频标题
     * @return 查询失败时返回空List对象
     */
    @Select(list + " where v.video_title like concat('%', #{videoTitle}, '%')")
    List<Video> selectLikeVideoTitle(String videoTitle);



/*    *//**
     * 查询Video记录
     *
     * @param video 用户实体
     * @return 多条Video记录
     *//*
    @Results({
            @Result(id = true, property = "videoId", column = "video_id"),
            @Result(property = "chapters", column = "video_id",
                    many = @Many(select = "com.jyb.mapper.ChapterMapper.selectByVideoId"))
    })
    @Select("<script>" +
            "select * " +
            "from lesson.video" +
            "<where>" +
            "<if test='videoId != null'> and video_id = #{videoId}</if>" +
            "<if test='videoTitle != null'> and video_title = #{videoTitle}</if>" +
            "<if test='author != null'> and author = #{author}</if>" +
            "<if test='videoInfo != null'> and video_info = #{videoInfo}</if>" +
            "<if test='summaryImage != null'> and summary_image = #{summaryImage}</if>" +
            "<if test='coverImage != null'> and cover_image = #{coverImage}</if>" +
            "<if test='price != null'> and price = #{price}</if>" +
            "<if test='star != null'> and star = #{star}</if>" +
            "<if test='createTime != null'> and create_time = #{createTime}</if>" +
            "<if test='modifyTime != null'> and modify_time = #{modifyTime}</if>" +
            " and video_deleted = 0" +
            "</where>" +
            "</script>")
    List<Video> select(Video video);
    *//*查询全部视频*//*
    @Select("select * from lesson.video where video_deleted =0 order by video_id")
    List<Video>selectAllVideo();

    *//*按照主键单个查询*//*
    @Select("select * from lesson.video where video_deleted=0 and video_id=#{param1}")
    Video selectByVideoId(Integer videoId);

    *//*添加*//*
    @Insert("insert into lesson.video(video_title,author,video_info,summary_image,cover_image,price,star,video_deleted,create_time,modify_time)" +
            "value(#{videoTitle},#{author},#{videoInfo},#{summaryImage},#{coverImage},#{price},#{star},#{videoDeleted},#{createTime},#{modifyTime})")
    int insertVideo(Video video);

    *//*修改*//*
    String updateVideo="<script> update lesson.video "
            +"<set>"
            +"<if test='videoTitle!=null'>video_title=#{videoTitle},</if>"
            +"<if test='author!=null'>author=#{author},</if>"
            +"<if test='videoInfo!=null'>video_info=#{videoInfo},</if>"
            +"<if test='summaryImage!=null'>summary_image=#{summaryImage},</if>"
            +"<if test='coverImage!=null'>cover_image=#{coverImage},</if>"
            +"<if test='price!=null'>price=#{price},</if>"
            +"<if test='star!=null'>star=#{star},</if>"
            +"<if test='videoDeleted!=null'>video_deleted=#{videoDeleted},</if>"
            +"<if test='createTime!=null'>create_time=#{createTime},</if>"
            +"<if test='modifyTime!=null'>modify_time=#{modifyTime},</if>"
            +"</set>"
            +" where video_deleted=0 and video_id=#{videoId}"
            +"</script>";
    @Update(updateVideo)
    int updateByVideoId(Video videoId);

    //property对应的是Java中的实体类属性，column对应数据库里面的属性
    @Results({
            //第一条@Result是为了加速用的，可以不配
            @Result(id = true, property = "videoId", column = "video_id"),
            //property找实体类里面的属性,column传一个数据到另一个Mapper里面并执行对应的方法
            @Result(property = "chapterList", column = "video_id",
                    many = @Many(select = "com.lyy.mapper.ChapterMapper.selectByVideoId"))
    })
    *//*根据videoId查询到chapter表里的数据*//*
    @Select("select * from lesson.video where video_id=#{param1} and video_deleted=0")
    Video selectByVideoIdAndChapter(Integer videoId);

    *//*删除*//*
    @Delete("delete lesson.video where video_id=#{param1}")
    int deleteByVideoId(Integer videoId);
    @Select("<script> " +
            "select * from lesson.video <where>" +
            "<foreach collection='array' item='videoId' "+
            "open='video_id in (' close=')' separator=','>"+
            "${videoId}"+
            "</foreach>"+
            "</where>"+
            " </script>")
    List<Video>selectByVideoIds(Integer[] videoIds);*/
}
